﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fast.Framework.Interfaces;

namespace Fast.Framework
{

    /// <summary>
    /// 更新建造者
    /// </summary>
    public class UpdateBuilder : IUpdateBuilder
    {

        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 来自
        /// </summary>
        public List<string> From { get; }

        /// <summary>
        /// 设置
        /// </summary>
        public string Set { get; set; }

        /// <summary>
        /// 条件
        /// </summary>
        public List<string> Where { get; }

        /// <summary>
        /// 是否批量
        /// </summary>
        public bool IsBatch { get; set; }

        /// <summary>
        /// 参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; }

        /// <summary>
        /// 参数列表
        /// </summary>
        public List<Dictionary<string, object>> ParametersList { get;  }

        /// <summary>
        /// 构造方法
        /// </summary>
        public UpdateBuilder()
        {
            From = new List<string>();
            Where = new List<string>();
            Parameters = new Dictionary<string, object>();
            ParametersList = new List<Dictionary<string, object>>();
        }

        /// <summary>
        /// 到字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append($"UPDATE {TableName} SET {Set}");
            if (From.Count > 0)
            {
                sb.Append($" FROM {string.Join(",", From)}");
            }
            if (Where.Count > 0)
            {
                sb.Append($" WHERE {string.Join(" AND ", Where)}");
            }
            //Console.WriteLine(sb.ToString().Trim());
            return sb.ToString().Trim();
        }
    }
}
